var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');

var app = getApp();
Page({
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
    // wx.switchTab({
    //   url: '/pages/ucenter/index/index'
    // })
  },
  wxLogin: function (e) {
   
    if (e.detail.userInfo == undefined){
      app.globalData.hasLogin = false;
      util.showErrorToast('微信登录失败');
      return;
    }
    console.log('......1.......')
    user.checkLogin().catch(() => {
      console.log('.....2........')
      let upperId = wx.getStorageSync('upperId')
      console.log('微信登录的uupperId：'+ upperId)
      user.loginByWeixin(e.detail.userInfo,upperId).then(res => {
        app.globalData.hasLogin = true;

        wx.navigateBack({
          delta: 1
        })
      }).catch((err) => {
        app.globalData.hasLogin = false;
        util.showErrorToast('微信登录失败');
      });

    });
  },
  accountLogin: function () {
    wx.navigateTo({ url: "/pages/auth/accountLogin/accountLogin" });
  }
})